// pages/question-detail/index.js
var app = getApp();

const Toptips = require('../../dist/toptips/index');
var CommonUtils = app.CommonUtils;

Page(Object.assign({}, Toptips, {
  /**
   * 页面的初始数据
   */
	data: {
		datas: "",
		datas_list: "",
		answerValue: {				//填写的答案
			cursor: 0,
			value: ''
		},
		isAnonymous: false,  	//是否匿名
		needIntegralValue: {	//要收取的积分
			cursor: 0,
			value: ''
		},
		imageList: [],
		isPublishBtnClick: false,//发布按钮是否可用
		isOpenMoneyDes: false,//是否展开积分与手续费说明
		isShowBigImageView: false,//是否显示大图展示
		showBigImageSrc: '',  //大图链接
    qid:'',
	},

  /**
   * 生命周期函数--监听页面加载
   */
	onLoad: function (options) {
		var _this = this;
    _this.setData({
      qid: options.id,
    })
  console.log("vvvvvvvcccccccffff");
    console.log(options.id);
		wx.request({
      url: "https://www.chnode.xyz/api/Question_Detail.ashx?",
			data: {
				DataType: "Question",
				qid: options.id,
			},
			success: function (res) {
				console.log(res);
				if (res.data.isok == "false")
					_this.showZanTopTips(res.data.info);
				else {
					_this.setData({
						datas: res.data.datas
					})
				}
			}
		})
		// wx.request({
		// 	url: "http://172.18.1.155:8058/api/Question_Detail.ashx?",
		// 	data: {
		// 		DataType: "Answer_List",
		// 		qid: "24",
		// 	},
		// 	success: function (res) {
		// 		console.log(res);
		// 		if (res.data.isok == "false")
		// 			_this.showZanTopTips(res.data.info);
		// 		else {
		// 			_this.setData({
		// 				datas_list: res.data.datas
		// 			})
		// 		}
		// 	}
		// })
	},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
	onReady: function () {

	},

  /**
   * 生命周期函数--监听页面显示
   */
	onShow: function () {

	},

  /**
   * 生命周期函数--监听页面隐藏
   */
	onHide: function () {

	},

  /**
   * 生命周期函数--监听页面卸载
   */
	onUnload: function () {

	},

	/**
	 * 监听答案输入
	 */
	bindInputAnswer: function (e) {
		var _this = this;
		_this.setData({
			answerValue: e.detail
		})
		_this.monitorPublishButtonStyle();
	},
	/**
	 * 监听积分输入
	 */
	bindInputNumber: function (e) {
		var _this = this;
		_this.setData({
			needIntegralValue: e.detail
		})
		_this.monitorPublishButtonStyle();
	},

	/**
	 * 监听发布按钮样式
	 */
	monitorPublishButtonStyle: function () {
		var _this = this;
		if (_this.data.answerValue.cursor >= 5 && _this.data.needIntegralValue.cursor > 0) {
			_this.setData({
				isPublishBtnClick: true
			})
		} else {
			_this.setData({
				isPublishBtnClick: false
			})
		}
	},

	/**
	 * 是否匿名点击事件
	 */
	btnEventIsAnonymous: function (e) {
		var _this = this;
		_this.setData({ isAnonymous: !_this.data.isAnonymous })
	},
	/**
	 * 是否展开说明点击事件
	 */
	btnEventOpenMoneyDes: function (e) {
		var _this = this;
		_this.setData({ isOpenMoneyDes: !_this.data.isOpenMoneyDes })
	},

	/**
	 * 点击选择图片按钮事件
	 */
	btnEventSelectImage: function () {
		var _this = this;
		wx.showActionSheet({
			itemList: ['相册', '拍照'],
			itemColor: "#F75F47",
			success: function (res) {
				if (!res.cancel) {
					if (res.tapIndex == 0) {
						_this.chooseWxImage('album')
					} else if (res.tapIndex == 1) {
						_this.chooseWxImage('camera')
					}
				}
			}
		})
	},

	/**
	 * 选择图片
	 */
	chooseWxImage: function (type) {
		var _this = this;
		wx.chooseImage({
			count: 1, // 默认9
			sizeType: ['original', 'compressed'],
			sourceType: [type],
			success: function (res) {
				var imgsArr = _this.data.imageList;
				imgsArr.push(res.tempFilePaths[0]);
				_this.setData({
					imageList: imgsArr
				})
			}
		})
	},

	/**
	 * 隐藏大图展示点击事件
	 */
	btnEventHiddenBigImage: function (e) {
		this.setData({
			isShowBigImageView: false,
			showBigImageSrc: ''
		})
	},

	/**
	 * 显示大图点击事件
	 */
	btnEvnetShowBigImage: function (e) {
		var _this = this;
		var index = e.currentTarget.dataset.index;
		_this.setData({
			showBigImageSrc: _this.data.imageList[index],
			isShowBigImageView: true,
		})
	},


	/**
	 * 发布点击事件
	 */
	fabuClicked: function (e) {
		var _this = this;
    var _Token;
    try {
      var value = wx.getStorageSync('Token')
      if (value) {
        // Do something with return value
        _Token = value;
      }
    } catch (e) {
      // Do something when catch error
    }
		console.log(_this.data.answerValue.value);
    console.log("111111");
    console.log(_Token);
    console.log("2222222");

		if (_this.data.isPublishBtnClick == false) {
			console.log("false");
		} else {
		
			console.log("true");
			wx.request({
        url: "https://www.chnode.xyz/api/Question_Answer.ashx?",
				data: {
					DataType: "Answer_Reply",
					token: _Token,
					qid: _this.data.qid,
					note: _this.data.answerValue.value,
					isAnonymity: _this.data.isAnonymous == true ? 1 : 0,
					integral: _this.data.needIntegralValue.value

				},
				success: function (res) {
					console.log(res);
					if (res.data.isok == "false") {
            CommonUtils.showToastWithImg(res.data.info, 2)


					} else {
						_this.setData({
							datas: res.data.datas

        
						})

console.log("fffffffffbbbbbbb");
            console.log(res.data.data.A_ID );

            _this.uploadImages(res.data.data.A_ID ,_this.data.imageList, 1);

            wx.showModal({
              title: '提示',
              content: '发布成功',
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定');
                  wx.navigateBack({
                    delta: 1
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })  


					}
				}
			})

		}
	},

	/**
	 * 上传图片
	 */
	uploadImages: function (temp_id, imageList, index) {
		var _this = this;
		if (index >= imageList.length + 1)
			return;
		wx.showLoading()
    var temp_url = 'https://www.chnode.xyz/api/Answer.ashx?DataType=AImageUpload&A_ID=' + temp_id + '&AI_Order=' + index + '&file=';
		console.log(temp_url)
		console.log(index)
		wx.uploadFile({
			url: temp_url,
			filePath: imageList[index - 1],
			name: 'file',
			success: function (res) {
				console.log('success')
				console.log(index)
				console.log(res.data)
			},
			fail: function (res) {
				console.log('fail')
				console.log(index)
				console.log(res.data)
			},
			complete: function (res) {
				if (index == imageList.length)
					wx.hideLoading();
				else {
					index = index + 1;
					_this.uploadImages(temp_id, imageList, index)
				}
			},
		})
	}


}))